import 'package:ai_desktop_eidtor/models/BookChapterRepository.dart';
import 'package:ai_desktop_eidtor/models/hive_models.dart';
import 'package:ai_desktop_eidtor/util/string_util.dart';
import 'package:ai_desktop_eidtor/util/y_req.dart';
import 'package:ai_desktop_eidtor/util/y_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

import '../../routes/app_page.dart';
import 'create_state.dart';

class CreateLogic extends GetxController {
  final CreateState state = CreateState();
  TextEditingController introField = TextEditingController();
  TextEditingController titleField = TextEditingController();

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
  }

  void createBook() async {
    if (titleField.text.isEmpty) {
      YToast.toast('请输入书籍名字');
      return;
    }
    yPost(
      load: (repository) async {
        String bookId = StringUtil.getBookId();
        final book = BookModel(
          id: bookId,
          title: titleField.text,
          author: StringUtil.userModel.authorName,
          description: introField.text,
        );
        await repository.addBook(book);
        repository.close();
        Get.offNamed(RouteName.bookDetail, arguments: bookId);
      },
    );
  }
}
